package com.ztom.top100;

/**
 * 排序链表
 * <p>
 * https://leetcode-cn.com/problems/sort-list/
 *
 * @author ZhangTao
 */
public class Code53SortList {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    /**
     * 归并排序(递归)
     *
     * @param head
     * @return
     */
    public ListNode sortList(ListNode head) {
        if (head == null) {
            return null;
        }
        return mergeSort(head);
    }

    private ListNode mergeSort(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode mid = findMid(head);
        // 一定要断开链表, 不然会死循环
        ListNode right = mid.next;
        mid.next = null;
        ListNode l1 = mergeSort(head);
        ListNode l2 = mergeSort(right);
        return merge(l1, l2);
    }

    private ListNode merge(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        ListNode i = l1;
        ListNode j = l2;
        while (i != null && j != null) {
            if (i.val <= j.val) {
                cur.next = i;
                i = i.next;
            } else {
                cur.next = j;
                j = j.next;
            }
            cur = cur.next;
        }
        while (i != null) {
            cur.next = i;
            i = i.next;
            cur = cur.next;
        }
        while (j != null) {
            cur.next = j;
            j = j.next;
            cur = cur.next;
        }
        return dummy.next;
    }

    private ListNode findMid(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        // 快慢指针找到中间节点
        ListNode slow = head, fast = head;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    /**
     * 归并排序(迭代)
     *
     * @param head
     * @return
     */
    public ListNode sortList1(ListNode head) {
        if (head == null) {
            return null;
        }

        ListNode l1 = null;
        ListNode l2 = null;
        ListNode dummy = new ListNode(-1, head);
        int len = getLength(head);
        int mergeSize = 1;
        while (mergeSize < len) {
            // 记录已合并的部分
            ListNode pre = dummy;
            // 每次从头开始
            ListNode cur = dummy.next;
            while (cur != null) {
                // 切分左边链表, 要保证节点数足够
                l1 = cur;
                for (int i = 1; i < mergeSize && cur.next != null; i++) {
                    cur = cur.next;
                }
                // 切分右边链表
                l2 = cur.next;
                // 断开链表
                cur.next = null;
                cur = l2;
                // 节点可能不够
                for (int i = 1; i < mergeSize && cur != null && cur.next != null; i++) {
                    cur = cur.next;
                }
                // 记录剩余的节点, 下一轮继续拆分, 然后断开链表
                ListNode last = null;
                if (cur != null) {
                    last = cur.next;
                    cur.next = null;
                }
                // 记录归并结果, 并将指针移到结尾
                pre.next = merge(l1, l2);
                while (pre.next != null) {
                    pre = pre.next;

                }
                // 准备下一轮
                cur = last;
            }

            mergeSize <<= 1;
        }

        return dummy.next;
    }

    private int getLength(ListNode head) {
        int len = 0;
        while (head != null) {
            len++;
            head = head.next;
        }
        return len;
    }

    /**
     * 快排(递归) todo
     *
     * @param head
     * @return
     */
    public ListNode sortList2(ListNode head) {
        if (head == null) {
            return null;
        }

        return null;
    }
}
